package com.lemon.smartsafetynew.ui.fragment.contact;

import android.os.Bundle;

import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.app.RefreshType;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafetynew.ui.fragment.base.BasePullToRefreshFragment;
import com.lemon.smartsafety.utils.app.HUDUtil;
import com.lemon.smartsafetynew.entity.User;
import com.lemon.smartsafetynew.entity.contact.DepartmentVo;
import com.lemon.smartsafetynew.entity.contact.PersonVo;
import com.lemon.smartsafetynew.entity.event.common.ViewEvent;
import com.lemon.smartsafetynew.entity.event.contact.PersonEvent;
import com.lemon.smartsafetynew.net.services.ContactServices;
import com.lemon.smartsafetynew.ui.adapter.contact.PersonListAdapter;
import com.lemon.smartsafetynew.util.constants.KeyConstants;
import com.lemon.smartsafetynew.util.user.AccountUtil;

import java.util.List;

import kale.adapter.LemonRcvAdapter;

/**
 * Created by Andrew on 2017/9/11.
 */

public class PersonListFragment extends BasePullToRefreshFragment<PersonVo, List<PersonVo>>
        implements RecyclerTClickListener<PersonVo> {

    private User user;

    private static String tag = "";
    private static DepartmentVo departmentVo;
    private static String company_id = "";

    @Override
    protected void getBaseArguments() {
        super.getBaseArguments();

        Bundle bundle = getArguments();

        if (bundle != null) {

            if (bundle.containsKey(KeyConstants.KEY_TAG)) {
                tag = bundle.getString(KeyConstants.KEY_TAG);
            }

            if (bundle.containsKey(KeyConstants.KEY_DEPARTMENT)) {
                departmentVo = (DepartmentVo) bundle.getSerializable(KeyConstants.KEY_DEPARTMENT);
            }
            if (bundle.containsKey(KeyConstants.KEY_COMPANY_ID)) {
                company_id = bundle.getString(KeyConstants.KEY_COMPANY_ID);
            }
        }
    }

    public static PersonListFragment newInstance(DepartmentVo departmentVo, String tag, String company_id) {

        Bundle args = new Bundle();
        args.putSerializable(KeyConstants.KEY_DEPARTMENT, departmentVo);
        args.putString(KeyConstants.KEY_TAG, tag);
        args.putString(KeyConstants.KEY_COMPANY_ID, company_id);

        PersonListFragment fragment = new PersonListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onPullRefresh() {

        LemonRequestParams params = new LemonRequestParams("");
        params.put("company_id", company_id);
        params.put("pageSize", pageSize);
        params.put("pageNum", currentPage);

        ContactServices
                .init()
                .getUserFromDepartment(RefreshType.REFRESH, params);

    }

    @Override
    public void onLoadMore() {

        LemonRequestParams params = new LemonRequestParams("");
        params.put("company_id", company_id);
        params.put("pageSize", pageSize);
        params.put("pageNum", currentPage);

        ContactServices
                .init()
                .getUserFromDepartment(RefreshType.LOAD_MORE, params);

    }

    @Override
    public boolean goRefreshOnCreate() {
        return true;
    }

    @Override
    public LemonRcvAdapter<PersonVo> getLemonAdapter() {
        return new PersonListAdapter(mDataList, this);
    }

    @Override
    public String getHttpTag() {
        return KeyConstants.URL_GET_COMPANY_PERSON;
    }

    @Override
    public void onItemClick(PersonVo data, int position) {

        PersonEvent personEvent = new PersonEvent();
        personEvent.setPersonVo(data);
        personEvent.setTag(tag);
        AppService.getBus().post(personEvent);
        AppService.getBus().post(new ViewEvent(PersonListFragment.class.getName()));
    }

    @Override
    protected void initViews() {
        super.initViews();

        initData();
    }

    /**
     * 初始化数据变量
     */
    private void initData() {

        user = AccountUtil.getUser();

        if (user == null) {
            mHUDUtil.showErrorTip("用户信息为空，请重新登陆！", new HUDUtil.CallBack() {
                @Override
                public void onCall() {

                }
            });
        }
    }
}
